package com.project.service;

import com.project.dao.*;
import com.project.entity.*;
import com.project.myBeans.ExerciseDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(rollbackFor = {Exception.class})
@Service
public class ExerciseServiceImpl extends BaseService implements ExerciseService {

    @Autowired
    ExerciseInfoMapper exerciseInfoMapper;

    @Autowired
    ChoiceMapper choiceMapper;

    @Autowired
    TrueFalseMapper trueFalseMapper;

    @Autowired
    ShortAnswerMapper shortAnswerMapper;

    @Autowired
    CompletionMapper completionMapper;

    @Autowired
    CompAnswerMapper compAnswerMapper;

    @Autowired
    ProgrammingMapper programmingMapper;

    /**
     * @author wwh
     * @date 2018/05/09
     * @function 添加单个题目
     * @param exerciseDetail
     */
    @Override
    public void add(ExerciseDetail exerciseDetail) {
        ExerciseInfo exerciseInfo = exerciseDetail.getExerciseInfo();
        exerciseInfoMapper.insertSelective(exerciseInfo);
        int exerciseInfoId = exerciseInfo.getId();
        switch (exerciseInfo.getType()){
            case 1: //单选
            case 2: //多选
                Choice choice = exerciseDetail.getChoice();
                choice.setExerinfoid(exerciseInfoId);
                choiceMapper.insertSelective(choice);
                break;
            case 3:
                TrueFalse trueFalse = exerciseDetail.getTrueFalse();
                trueFalse.setExerinfoid(exerciseInfoId);
                trueFalseMapper.insertSelective(trueFalse);
                break;
            case 4:
                Completion completion = exerciseDetail.getCompletion();
                completion.setExerinfoid(exerciseInfoId);
                completionMapper.insertSelective(completion);
                for(int i = 0; i < completion.getAnswerArray().length;i++){
                    CompAnswer compAnswer = new CompAnswer();
                    compAnswer.setCompid(completion.getId());
                    compAnswer.setIndex(i+1);   //设置填空序号，从1开始
                    compAnswer.setAnswer(completion.getAnswerArray()[i]);
                    compAnswerMapper.insertSelective(compAnswer);
                }
                break;
            case 5:
                ShortAnswer shortAnswer = exerciseDetail.getShortAnswer();
                shortAnswer.setExerinfoid(exerciseInfoId);
                shortAnswerMapper.insertSelective(shortAnswer);
                break;
            case 6:
                Programming programming = exerciseDetail.getProgramming();
                programming.setExerinfoid(exerciseInfoId);
                programmingMapper.insertSelective(programming);
                break;
        }
    }
}
